package com.mzx.javase.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 类描述：对象流专属：将对象持久化到硬盘
 *
 * @ClassName ObjectOutputStreamTest
 * @Description TODO
 * @Author 马子鑫
 * @Date 2022/7/31 20:23
 * @Version 1.0
 */
public class ObjectOutputStreamTest {
    public static void main(String[] args) {
        ObjectOutputStream oop = null;
        try {
            oop = new ObjectOutputStream(new FileOutputStream("emploee.txt"));
            Employee employee = new Employee("张三", "北京");
            oop.writeObject(employee);
            oop.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (oop != null) {
                try {
                    oop.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
final class Employee implements Serializable {

    private static final long serialVersionUID = 8784679603887841021L;
    private String name;
    private transient String address;

    private int age;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public Employee() {
    }

    public Employee(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
